﻿<%@ Page Language="C#" %>

<!DOCTYPE html>
<script runat="server">
    
        static CommonLib.DbHelper.IDBhelper dbHelper = new CommonLib.DbHelper.Factory().IDBhelper;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind_data();
            }
        }

        private void DataBind_data()
        {
            var sdr = dbHelper.ExecuteReader("SELECT * FROM " + Request["tn"].ToString() + " WHERE id = '" + Request["id"].ToString() + "'");
            if (sdr.Read())
            {
                标识Literal.Text = sdr["id"].ToString();
                父标识Literal.Text = sdr["pid"].ToString();
                级别Literal.Text = sdr["Grade"].ToString();
                叶子Literal.Text = sdr["Leaf"].ToString();

                层次Literal.Text = sdr["Levels"].ToString();
                名称TextBox.Text = sdr["Name"].ToString();
                链接TextBox.Text = sdr["Url"].ToString();
                值TextBox.Text = sdr["Value"].ToString();

                setDropDownList(禁用DropDownList, sdr["Disable"].ToString(), "否");
                setDropDownList(节点状态DropDownList, sdr["State"].ToString(), "open"); 

                if (sdr["Leaf"].ToString().ToLower() == "true")
                {
                    节点状态DropDownList.Enabled = false;
                }
            }
            sdr.Close();
        }

        private void setDropDownList(DropDownList list, string value, string defaultValue)
        {
            try
            {
                list.Items.FindByValue(value).Selected = true;
            }
            catch
            {
                //list.Items.FindByValue(defaultValue).Selected = true;
            }
        }

        protected void 重置Button_Click(object sender, EventArgs e)
        {
            DataBind_data();
        }

        protected void 提交Button_Click(object sender, EventArgs e)
        {
            SqlParameter[] P = {
                                    new SqlParameter("@id", Request["id"].ToString()),
                                    new SqlParameter("@pid", 父标识Literal.Text),
                                    new SqlParameter("@Grade", 级别Literal.Text),
                                    new SqlParameter("@Leaf", 叶子Literal.Text),
                                    new SqlParameter("@Levels", 层次Literal.Text),
                                    new SqlParameter("@Name", 名称TextBox.Text), 
                                    new SqlParameter("@Url", 链接TextBox.Text),
                                    new SqlParameter("@Disable", 禁用DropDownList.Text) ,
                                    new SqlParameter("@State", 节点状态DropDownList.Text), 
                                    new SqlParameter("@Value", 值TextBox.Text) 
                                };

            if (Request["tn"].ToString() == "System_Menu")
            {
                dbHelper.ExecuteNonQuery(@"UPDATE " + Request["tn"].ToString() + @"
                                    SET pid=@pid, Grade=@Grade, Leaf=@Leaf, Levels=@Levels, Name=@Name, Url=@Url,Disable=@Disable,State=@State,Value=@Value
                                    WHERE (id = @id)", P);
            }
            else
            {
                dbHelper.ExecuteNonQuery(@"UPDATE " + Request["tn"].ToString() + @"
                                    SET pid=@pid, Grade=@Grade, Leaf=@Leaf, Levels=@Levels, Name=@Name,Url=@Url,Disable=@Disable,State=@State,Value=@Value
                                    WHERE (id = @id)", P);
            }

            //更新父级页面节点名称
            JSLiteral.Text = "<script>";
            JSLiteral.Text += "var node = parent.$(\"#tree\", window.parent.document).tree(\"find\", \"" + Request["id"].ToString() + "\");";
            JSLiteral.Text += "node.text = \"" + 名称TextBox.Text + "\";";
            JSLiteral.Text += "parent.$(\"#tree\", window.parent.document).tree(\"update\", node);";
            JSLiteral.Text += "</" + "script>";

            DataBind_data();
        }
</script>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>树属性管理</title>  
    <style type="text/css">
        .btnCancel { margin-left: 6px; }
        .input { width: 450px; padding: 3px; }
        .icons { float: left; margin: 3px; border: 2px solid #F2F7FC; }
        .iconsxz { float: left; margin: 3px; border: 2px solid red; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table cellspacing="10" cellpadding="0" border="0" style="background-color: #F2F7FC; border: #CCE0F5 solid 1px; font-size: 12px; width: 100%;">
            <tr style="background-color: #F2F7FC;">
                <td colspan="2">
                    <table width="100%">
                        <tr>
                            <td width="100px" align="right">标识：</td>
                            <td>
                                <asp:Literal ID="标识Literal" runat="server" /></td>
                        </tr>
                        <tr style="display: none;">
                            <td width="100px" align="right">父标识：</td>
                            <td>
                                <asp:Literal ID="父标识Literal" runat="server" /></td>
                        </tr>
                        <tr style="display: none;">
                            <td width="100px" align="right">叶子：</td>
                            <td>
                                <asp:Literal ID="叶子Literal" runat="server" /></td>
                        </tr>
                        <tr style="display: none;">
                            <td width="100px" align="right">级别：</td>
                            <td>
                                <asp:Literal ID="级别Literal" runat="server" /></td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">层次：</td>
                            <td>
                                <asp:Literal ID="层次Literal" runat="server" /></td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">名称：</td>
                            <td>
                                <asp:TextBox ID="名称TextBox" runat="server" CssClass="input" Width="600px" /></td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">值：</td>
                            <td>
                                <asp:TextBox ID="值TextBox" runat="server" CssClass="input" Width="600px" /></td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">链接：</td>
                            <td>
                                <asp:TextBox ID="链接TextBox" runat="server" CssClass="input" Width="600px" /></td>
                        </tr> 
                        <tr style="display: none;">
                            <td width="100px" align="right">禁用：</td>
                            <td>
                                <asp:DropDownList ID="禁用DropDownList" runat="server">
                                    <asp:ListItem>是</asp:ListItem>
                                    <asp:ListItem>否</asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">节点状态：</td>
                            <td>
                                <asp:DropDownList ID="节点状态DropDownList" runat="server">
                                    <asp:ListItem Text="关闭" Value="closed"></asp:ListItem>
                                    <asp:ListItem Text="打开" Value="open"></asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr> 
                        <tr>
                            <td width="100px" align="right"></td>
                            <td>
                                <asp:Button ID="重置Button" runat="server" Text="重 置" OnClick="重置Button_Click" />
                                &nbsp;<asp:Button ID="提交Button" runat="server" Text="提 交" OnClick="提交Button_Click" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <asp:Literal ID="JSLiteral" runat="server" />
    </form>
</body>
</html>
